package com.bdqn.servlet;

import com.alibaba.fastjson.JSON;
import com.bdqn.pojo.Student;
import com.bdqn.service.StudentService;
import com.bdqn.service.StudentServiceImpl;
import com.bdqn.util.SimpleBeanConvertor;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * StudentServlet
 *
 * @author LILIBO
 * @since 2021-09-15
 */
@WebServlet("/student")
public class StudentServlet extends HttpServlet {

    private StudentService studentService = new StudentServiceImpl();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        /*String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        Person queryPerson = new Person();
        queryPerson.setName(name);
        queryPerson.setSex(sex);*/

        // 从请求对象中获取参数
        Map<String, String[]> params = request.getParameterMap();
        String json = null;
        try {
            Student query = SimpleBeanConvertor.convert(params, Student.class);
            List<Student> list = studentService.getList(query);
            json = JSON.toJSONString(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.setAttribute("obj", json);
        request.getRequestDispatcher("main.jsp").forward(request, response);
    }
}
